package com.aibaixun.system.service.impl;

import com.aibaixun.common.core.domain.R;
import com.aibaixun.common.utils.BeanCopyUtils;
import com.aibaixun.common.utils.StringUtils;
import com.aibaixun.system.domain.dto.PostureDeviceModelPageDto;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aibaixun.system.domain.PostureDeviceModel;
import com.aibaixun.system.service.PostureDeviceModelService;
import com.aibaixun.system.mapper.PostureDeviceModelMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 91516
* @description 针对表【posture_device_model(U模型信息表)】的数据库操作Service实现
* @createDate 2024-07-23 18:18:42
*/
@Service
public class PostureDeviceModelServiceImpl extends ServiceImpl<PostureDeviceModelMapper, PostureDeviceModel>
    implements PostureDeviceModelService{

    @Override
    public R<Page<PostureDeviceModel>> getPage(Page page, PostureDeviceModelPageDto pageDto) {
        Page<PostureDeviceModel> resultData = this.baseMapper.selectPage(page,
            new LambdaQueryWrapper<PostureDeviceModel>()
            .like(StringUtils.isNotBlank(pageDto.getModelName()),PostureDeviceModel::getModelName,pageDto.getModelName())
        );
        return R.ok(resultData);
    }

    @Override
    public R<List<PostureDeviceModel>> getList(PostureDeviceModel dto) {
        List<PostureDeviceModel> resultData = this.baseMapper.selectList(
            new LambdaQueryWrapper<PostureDeviceModel>()
                .like(StringUtils.isNotBlank(dto.getModelName()),PostureDeviceModel::getModelName,dto.getModelName())
        );
        return R.ok(resultData);
    }

    @Override
    public R add(PostureDeviceModel dto) {
        Long count = this.baseMapper.selectCount(
            new LambdaQueryWrapper<PostureDeviceModel>()
            .eq(PostureDeviceModel::getModelName,dto.getModelName())
        );
        if(count > 0){
            return R.fail("模型名称已存在");
        }
        this.baseMapper.insert(dto);
        return R.ok();
    }

    @Override
    public R update(PostureDeviceModel dto) {
        PostureDeviceModel model = this.baseMapper.selectById(dto.getModelId());
        if(model == null){
            return R.fail("模型信息异常");
        }
        if(StringUtils.isBlank(dto.getModelName()) && !StringUtils.equals(dto.getModelName(),model.getModelName())){
            Long count = this.baseMapper.selectCount(
                new LambdaQueryWrapper<PostureDeviceModel>()
                    .eq(PostureDeviceModel::getModelName,dto.getModelName())
            );
            if(count > 0){
                return R.fail("模型名称已存在");
            }
        }
        BeanCopyUtils.copy(dto,model);
        this.baseMapper.updateById(model);
        return R.ok();
    }

    @Override
    public R delete(Long modelId) {
        this.baseMapper.deleteById(modelId);
        return R.ok();
    }
}




